[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
virtual Virtual Function Modifier
The virtual modifier is used to ease the way for inheritance
procedures. You can have either virtual base classes or virtual
functions.
A virtual base class lets you specify a base class more than
once in a derived class -- if done indirectly. For example, this is
illegal because a base class can't be specified more than once in a
derived class:
class AAAA { ...};
class BBBB : AAAA, AAAA {statements;};
However, you can do this indirectly as follows:
class BBBB : public AAAA { ... }
class CCCC : public AAAA { ... }
class DDDD : public BBBB, public CCCC { ... } // OK
If the compiler objects, add the virtual modifier to make the base
classes virtual:
class BBBB : virtual public AAAA { ... }
class CCCC : virtual public AAAA { ... }
class DDDD : public BBBB, public CCCC { ... } // OK
Virtual functions, on the other hand, let derived classes
provide different versions of a base class function. The idea is to
define a virtual function in a base class and then redefine it in a
derived class. In this case, the virtual function finch_spotted() is
redefined in the derived class my_birdwatch:
class birdwatch {
int num_finches;
int num_seagulls;
public:
int virtual finch_spotted();
int virtual seagull_spotted();
};
class my_birdwatch : public birdwatch {
int num_blue_jays;
int num_sparrows;
int finch_spotted();
};
See Also:
private
protected
public
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson